package Question2_1;
import java.util.Hashtable;
import CareerCupLibrary.AssortedMethods;
import CareerCupLibrary.LinkedListNode;
public class Question {
public static void deleteDups(LinkedListNode n) {
Hashtable table = new Hashtable();
LinkedListNode previous = null;
while (n != null) {
if (table.containsKey(n.data)) {
previous.next = n.next;
} else {
table.put(n.data, true);
previous = n;
}
n = n.next;
}
}
public static void deleteDups2(LinkedListNode head) {
if (head == null) return;
LinkedListNode previous = head;
LinkedListNode current = previous.next;
while (current != null) {
// Look backwards for dups, and remove any that you see.
LinkedListNode runner = head;
while (runner != current) {
if (runner.data == current.data) {
LinkedListNode tmp = current.next;
previous.next = tmp;
current = tmp;
/* We know we can�t have more than one dup preceding
* our element since it would have been removed
* earlier. */
break;
}
runner = runner.next;
}
/* If runner == current, then we didn�t find any duplicate
* elements in the previous for loop. We then need to
* increment current.
* If runner != current, then we must have hit the �break�
* condition, in which case we found a dup and current has
* already been incremented.*/
if (runner == current) {
previous = current;
current = current.next;
}
}
}
public static void main(String[] args) {
LinkedListNode head = AssortedMethods.randomLinkedList(10, 0, 10);
System.out.println(head.printForward());
deleteDups2(head);
System.out.println(head.printForward());
}
}